package Campus.help.user.service.impl;

import Campus.help.common.utils.R;
import Campus.help.common.utils.RandomUtils;
import Campus.help.user.dao.MaintenanceWorkerInfoMapper;
import Campus.help.user.entity.MaintenanceWorkerInfo;
import Campus.help.user.service.MaintenanceWorkerInfoService;
import Campus.help.user.vo.WorkerInfoVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author haoyun.xiong
 * @since 2023-05-09
 */
@Service
public class MaintenanceWorkerInfoServiceImpl extends ServiceImpl<MaintenanceWorkerInfoMapper, MaintenanceWorkerInfo> implements MaintenanceWorkerInfoService {

    @Autowired
    private MaintenanceWorkerInfoMapper workerInfoMapper;
    @Autowired
    private PasswordEncoder passwordEncoder;
    @Override
    public R addWokerInfo(WorkerInfoVo vo) {
        MaintenanceWorkerInfo maintenanceWorkerInfo = new MaintenanceWorkerInfo();
        BeanUtils.copyProperties(vo,maintenanceWorkerInfo);
        maintenanceWorkerInfo.setPassword(passwordEncoder.encode(vo.getPassword()));
        maintenanceWorkerInfo.setJobNumber(Long.valueOf(RandomUtils.getFourBitRandom()));
        workerInfoMapper.insert(maintenanceWorkerInfo);
        return R.ok();
    }

    @Override
    public R updateWokerInfo(MaintenanceWorkerInfo vo) {
        vo.setPassword(passwordEncoder.encode(vo.getPassword()));
        workerInfoMapper.insert(vo);
        return R.ok();
    }

    @Override
    public R getWokerInfo() {
        List<MaintenanceWorkerInfo> infos = workerInfoMapper.selectList(null);
        return R.ok().setData(infos);
    }

    @Override
    public R delWokerInfo(Long id) {
        workerInfoMapper.delWokerInfo(id);
        return R.ok();
    }

    @Override
    public MaintenanceWorkerInfo getWokerByAcount(String userName) {
        return workerInfoMapper.getWokerByAcount(userName);
    }
}
